// pages/user/register/index.js
import requestUtil from '../../../baseRequest.js'
import urls from '../../../requestUrl.js'

var openid = wx.getStorageSync('openid')


Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid: openid,
        baseUrl: urls.baseUrl
    },
    goToRegister() {
        wx.navigateTo({
            url: '/pages/user/memberRegister/before',
        });
    },
    /**获取手机号码 */
    getPhoneNumber({ detail }) {
        if (detail.detail.encryptedData) {
            let param = detail.detail;
            param.openid = this.data.openid;
            requestUtil.postRequest(urls.user.getPhone, param, (response) => {
                this.setData({
                    telphone: response
                })
                wx.setStorageSync('telPhone', response);

            }, () => { })
        }
    },
    /**请求注册用户 */
    handleUserRegister() {
        let param = { 'openid': this.data.openid, 'telphone': this.data.telphone }
        requestUtil.postRequest(urls.user.register, param, (res) => {
            wx.showModal({
                title: '温馨提醒',
                content: '恭喜您，用户注册成功！',
                showCancel: false,
                confirmText: '返回'
            })
            getApp().updateGlobalUserRegisterStatus(1)

            param = {
                "headimgUrl": getApp().globalData.userInfo.headimgUrl,
                "nickname": getApp().globalData.userInfo.nickname,
                "openid": this.data.openid
            }
            getApp().getUserInfo(getApp().getUserOpenid(), () => {
                wx.navigateBack()
            })
            // this.setData()
        }, () => { })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _distributeCode = options.scene;
        if(_distributeCode){
            getApp().saveDistributeCode(_distributeCode);
        };
        wx.login({
            complete: ({ code }) => {
                requestUtil.getRequest(urls.user.getCode + code, {}, (res) => {
                    if (res && res.openid) {
                        this.setData({
                            openid: res.openid,
                        })
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
